﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentScheduler;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.EFDBFirst;

namespace YiYiTong.Rocket.ProcessConsole
{
    /// <summary>
    /// 业务消息
    /// </summary>
    public class BusinessMsgJob : BaseJob
    {
        static int excutecounts;
        
        public override void DoAction()
        {
            if (BusinessMsgJob.excutecounts % 100 == 0)
            {
                LogHelper.Debug($"业务消息发送：{BusinessMsgJob.excutecounts}次");
            }
            BusinessMsgJob.excutecounts++;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();


            var list = dbcontext.ADZ_BusinessMsg.Where(t => t.is_send == 0).ToList();
            if (list.Any())
            {
                list.ForEach(t => { t.is_send = 1; });
                dbcontext.SaveChanges();
                foreach (var item in list)
                {
                    var configlist = dbcontext.ADZ_BusinessMsg_UserConfig.Where(t => t.BusinessMsgType == item.BusinessMsgType).ToList();
                    if (configlist.Any())
                    {
                        configlist.ForEach(t =>
                        {
                            if (t.UID > 0)
                            {
                                var usermsg = new ADZ_BusinessMsg_SysUser
                                {
                                    BusinessMsgId = item.seq_id,
                                    CreateTime = DateTime.Now,
                                    IsDel = 0,
                                    IsNew = 0,
                                    IsRead = 0,
                                    UID = t.UID.Value
                                };
                                dbcontext.ADZ_BusinessMsg_SysUser.Add(usermsg);
                            }
                        });
                    }
                }
                dbcontext.SaveChanges();
            }
        }
    }
}
